angular.module('dDatepicker',[])
    .directive('dDatepicker', function ($timeout, $parse) {
        return {
            link: function ($scope, element, $attrs) {
                return $timeout(function () {
                    var ngModelGetter = $parse($attrs['ngModel']);
                    var option = {};
                    var defaulte = {
                        useCurrent: true,
                        format: 'YYYY-MM-DD HH:mm:ss',
                        defaultDate: new Date(),
                        keepOpen : true,
                        showTodayButton : true,
                        showClear : true,
                        sideBySide : false,
                        useStrict : true,
                        toolbarPlacement : 'default'
                    };
                    if ($attrs.dDatepickerOption) {
                        option = $scope.$eval($attrs.dDatepickerOption);
                        if (angular.isObject(option)) {
                            option = angular.extend(defaulte, option);
                        }
                    } else {
                        option = defaulte;
                    }
                    if(option.defaultDate){
                        ngModelGetter.assign($scope, moment(option.defaultDate).format(option.format));
                    }
                    return $(element).datetimepicker(option).on('dp.change', function (event) {
                            $scope.$apply(function () {
                                return ngModelGetter.assign($scope, $(event.target).find("input").val());
                            });
                        });
                });
            }
        };
    });